package com.gopher.gopherbeasts;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Art {
	@SuppressWarnings("rawtypes")
	private static HashMap table = new HashMap();

	public static Texture background;
	public static Texture player;
	public static Texture hit;
	public static TextureAtlas atlas;
	public static TextureRegion[][] tiles;

	public static void load() {
		atlas = new TextureAtlas(Gdx.files.internal("bin/textures.pack"));
		
		background = load("images/world/spacesmall.png");
		player = load("images/pack/player.png");
		hit = load("images/pack/hit.png");
		
		tiles = split("images/world/tiles.png", 32, 32, false);
	}
	private static TextureRegion[][] split(String name, int w, int h, boolean flip) {
		Texture texture = new Texture(Gdx.files.internal(name));
		int xSlices = texture.getWidth() / w;
		int ySlices = texture.getHeight() / h;
		TextureRegion[][] res = new TextureRegion[xSlices][ySlices];
		for (int x = 0; x < xSlices; x++) {
			for (int y = 0; y < ySlices; y++) {
				res[x][y] = new TextureRegion(texture, x * w, y * h, w, h);
				res[x][y].flip(flip, false);
			}
		}
		return res;
	}

	public static TextureRegion getTextureRegion(String resourceName) {
		String path = "images/pack/" + resourceName;
		TextureRegion textureRegion = (TextureRegion) table.get(path);

		if (textureRegion != null) {
			return textureRegion;
		}

		textureRegion = atlas.findRegion(path);
		return textureRegion;

	}

	private static Texture load(String path) {
		return new Texture(Gdx.files.internal(path));
	}
}
